// composables/useAboutApi.ts
import { useApi } from '@/composables/useApi'
import type { AboutUs, AboutData } from '~/types/about'

export const useAboutApi = () => {
  // 获取关于我们数据
  const getAboutData = async (): Promise<AboutUs> => {
    try {
      const response = await useApi<AboutData>('/api/about-us')

      // 检查业务逻辑是否成功
      if (!response.success) {
        throw {
          code: response.code,
          message: response.message,
          data: response.data
        }
      }

      return response.data as AboutUs
    } catch (error: any) {
      console.error('获取关于我们数据失败:', error)
      throw error // 继续抛出给组件处理
    }
  }

  // 更新关于我们数据
  const updateAboutData = async (updateData: Partial<AboutUs>) => {
    try {
      const response = await useApi<AboutData>('/api/about-us', {
        method: 'PUT',
        body: updateData
      })

      if (!response.success) {
        throw {
          code: response.code,
          message: response.message,
          data: response.data
        }
      }

      return response.data
    } catch (error: any) {
      console.error('更新关于我们数据失败:', error)
      throw error
    }
  }

  return {
    getAboutData,
    updateAboutData
  }
}